import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期工具
 * 
 * */
public class DateUtils {

	
	/**
	 * String转java.util.Date型 "yyyy-MM-dd"
	 * @param str
	 * @return
	 */
	public static Date getDate(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Date date = null;
		if (str != null && str.length() > 0) {
			try {
				date = sdf.parse(str);
			} catch (ParseException e) {
				date = null;
			}
		} else {
			date = null;
		}
		return date;
	}

	/**
	 * String转Date型 "yyyy-MM-dd, hh:mm:ss"
	 * @param str
	 * @return
	 */
	public static Date getAllDate(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = null;
		if (str != null && str.length() > 0) {
			try {
				date = sdf.parse(str);
			} catch (ParseException e) {

			}
		}
		return date;
	}

	/**
	 * String转Date型 "yyyy-MM-dd, hh:mm"
	 * @param str
	 * @return
	 */
	public static Date getDateMM(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
		Date date = null;

		if (str != null && str.length() > 0) {
			try {
				date = sdf.parse(str);
			} catch (ParseException e) {
				date = null;
			}
		} else {
			date = null;
		}
		return date;
	}

	/**
	 * String转Date型 "yyyy-MM-dd, hh"
	 * @param str
	 * @return
	 */
	public static Date getDateHH(String str) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH");
		Date date = null;

		if (str != null && str.length() > 0) {
			try {
				date = sdf.parse(str);
			} catch (ParseException e) {
				date = null;
			}
		} else {
			date = null;
		}
		return date;
	}

	/**
	 * Date转String "yyyy-MM-dd HH:mm:ss"
	 * @param date
	 * @return
	 */
	public static String getStringAlldate(Date date) {
		String time = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (date != null) {
			try {
				time = sdf.format(date);
			} catch (Exception e) {
				time = null;
			}
		} else {
			time = null;
		}
		return time;
	}

	/**
	 * Date转String "yyyy-MM-dd"
	 * @param date
	 * @return
	 */
	public static String getStrDate(Date date) {
		String time = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		if (date != null) {
			try {
				time = sdf.format(date);
			} catch (Exception e) {
				time = null;
			}
		} else {
			time = null;
		}
		return time;
	}

	/**
	 * 获取时间 年
	 * @param date
	 * @return
	 */
	public static String getYear(Date date) {
		String year = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy");
		if (date != null) {
			try {
				year = sdf.format(date);
			} catch (Exception e) {
				year = null;
			}
		} else {
			year = null;
		}
		return year;
	}

	/**
	 * 获取时间 季度
	 * @param date
	 * @return
	 */
	public static String getQuarter(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		double quarter = 0;
		String str = null;
		if (date != null) {
			try {
				quarter = Integer.valueOf(sdf.format(date));
				if (quarter / 3 <= 1) {
					str = "1";
				} else if (quarter / 3 <= 2) {
					str = "2";
				} else if (quarter / 3 <= 3) {
					str = "3";
				} else if (quarter / 3 <= 4) {
					str = "4";
				}
			} catch (Exception e) {
				str = null;
			}
		} else {
			str = null;
		}
		return str;
	}

	/**
	 * 获取时间 月份
	 * @param date
	 * @return
	 */
	public static String getMonth(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("MM");
		String month = null;
		if (date != null) {
			try {
				month = sdf.format(date);
			} catch (Exception e) {
				month = null;
			}
		} else {
			month = null;
		}
		return month;
	}

	/**
	 * 获取时间 天数
	 * @param date
	 * @return
	 */
	public static String getDay(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("dd");
		String day = null;
		if (date != null) {
			try {
				day = sdf.format(date);
			} catch (Exception e) {
				day = null;
			}
		} else {
			day = null;
		}
		return day;
	}

	/**
	 * 获取时间 周
	 * @param date
	 * @return
	 */
	public static String getWeek(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("ww");
		String week = null;
		if (date != null) {
			try {
				week = sdf.format(date);
			} catch (Exception e) {
				week = null;
			}
		} else {
			week = null;
		}
		return week;
	}

	/**
	 * 获取时间 天(系统日期是今年的第几天)
	 * @param date
	 * @return
	 */
	public static String getDayOfYear(Date date) {
		String day = null;
		if (date != null) {
			try {
				Calendar cal = Calendar.getInstance();
				Calendar.getInstance();
				cal.clear();
				cal.setTime(date);
				day = String.valueOf(cal.get(cal.DAY_OF_YEAR));
			} catch (Exception e) {
				day = null;
			}
		} else {
			day = null;
		}
		return day;
	}

	/**
	 * 获取年月(Date转String，类型："yyyyMM")
	 * @param date
	 * @return
	 */
	public static String getYearMonth(Date date) {
		String ym = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMM");
		if (date != null) {
			try {
				ym = sdf.format(date);
			} catch (Exception e) {
				ym = null;
			}
		} else {
			ym = null;
		}
		return ym;
	}

	/**
	 * 获取年月(Date转String，类型："yyyy-MM")
	 * @param date
	 * @return
	 */
	public static String getYM(Date date) {
		String ym = null;
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		if (date != null) {
			try {
				ym = sdf.format(date);
			} catch (Exception e) {
				ym = null;
			}
		} else {
			ym = null;
		}
		return ym;
	}

	/**
	 * String转Date 类型“yyyy-MM”
	 * @param str
	 * @return
	 */
	public static Date getDateYM(String str) {

		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date datenow = null;
		if (str != null && str.length() > 0) {
			try {
				Long sysdate = sdf.parse(str).getTime();
				datenow = new Date(sysdate);
			} catch (ParseException e) {
				datenow = null;
			}

		} else {
			datenow = null;
		}
		return datenow;

	}

	/**
	 * Date转Date 类型“yyyy-MM”
	 * @param str
	 * @return
	 */
	public static Date getDateYM(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
		Date datenow = null;
		if (date != null) {
			String str = DateUtils.getYM(date);
			try {
				Long sysdate = sdf.parse(str).getTime();
				datenow = new Date(sysdate);
			} catch (ParseException e) {
				datenow = null;
			}

		} else {
			datenow = null;
		}
		return datenow;

	}

	/**  
	 * 计算两个日期之间相差的月数  
	 * @param date1  
	 * @param date2  
	 * @return  
	 */
	public static int getMonths(Date date1, Date date2) {
		int iMonth = 0;
		int flag = 0;
		if (date1 != null && date2 != null) {
			try {
				Calendar objCalendarDate1 = Calendar.getInstance();
				objCalendarDate1.setTime(date1);

				Calendar objCalendarDate2 = Calendar.getInstance();
				objCalendarDate2.setTime(date2);

				if (objCalendarDate2.equals(objCalendarDate1))
					return 0;
				if (objCalendarDate1.after(objCalendarDate2)) {
					Calendar temp = objCalendarDate1;
					objCalendarDate1 = objCalendarDate2;
					objCalendarDate2 = temp;
				}
				if (objCalendarDate2.get(Calendar.DAY_OF_MONTH) < objCalendarDate1
						.get(Calendar.DAY_OF_MONTH))
					flag = 1;

				if (objCalendarDate2.get(Calendar.YEAR) > objCalendarDate1
						.get(Calendar.YEAR))
					iMonth = ((objCalendarDate2.get(Calendar.YEAR) - objCalendarDate1
							.get(Calendar.YEAR))
							* 12 + objCalendarDate2.get(Calendar.MONTH) - flag)
							- objCalendarDate1.get(Calendar.MONTH);
				else
					iMonth = objCalendarDate2.get(Calendar.MONTH)
							- objCalendarDate1.get(Calendar.MONTH) - flag;

			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return iMonth;
	}
	
	/**
	 * 计算两个日期天数差
	 * @param startDate
	 *        开始日期
	 *        
	 * @param endDate
	 *        结束日期   
	 * @return 天数       
	 * */
	public static int days(Date startDate, Date endDate) {
		  SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		  String startDateStr = sdf.format(startDate);
		  String endDateStr = sdf.format(endDate);
		  
		  int year1 = Integer.parseInt(startDateStr.substring(0, 4));
		  int month1 = Integer.parseInt(startDateStr.substring(4, 6));
		  int day1 = Integer.parseInt(startDateStr.substring(6, 8));
		  int year2 = Integer.parseInt(endDateStr.substring(0, 4));
		  int month2 = Integer.parseInt(endDateStr.substring(4, 6));
		  int day2 = Integer.parseInt(endDateStr.substring(6, 8));
		  Calendar c1 = Calendar.getInstance();
		  c1.set(Calendar.YEAR, year1);
		  c1.set(Calendar.MONTH, month1 - 1);
		  c1.set(Calendar.DAY_OF_MONTH, day1);
		  Calendar c2 = Calendar.getInstance();
		  c2.set(Calendar.YEAR, year2);
		  c2.set(Calendar.MONTH, month2 - 1);
		  c2.set(Calendar.DAY_OF_MONTH, day2);
		  long mills =
		   c1.getTimeInMillis() > c2.getTimeInMillis()
		    ? c1.getTimeInMillis() - c2.getTimeInMillis()
		    : c2.getTimeInMillis() - c1.getTimeInMillis();
		  return (int) (mills / 1000 / 3600 / 24);
   }
	
	/**
	 * 计算两个日期周数数差
	 * @param startDate
	 *        开始日期
	 *        
	 * @param endDate
	 *        结束日期   
	 * @return  周数　　
	 * */
	public static int weeks(Date d1, Date d2)  {
		long n1 = d1.getTime();
		long n2 = d2.getTime();
		long diff = Math.abs(n1 - n2);
		
		diff /= 3600 * 1000 * 24;
		if(diff % 7 != 0)
			return (int)(diff / 7 + 1)-1;
		return (int)(diff / 7)-1;
	}

	
	/**
	 * 得到周几
	 * @param date
	 *        日期
	 *          
	 * @return 周几　　
	 * */
	public static int dayForWeek(Date date)  {
		Calendar cal =  Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_WEEK);
	}
	
	/**
	 * 得到几号
	 * @param date
	 *        日期
	 *          
	 * @return 几号　　
	 * */
	public static int dayForMonth(Date date)  {
		Calendar cal =  Calendar.getInstance();
		cal.setTime(date);
		return cal.get(Calendar.DAY_OF_MONTH);
	}
	
	/**  
	 * 比较两日期大小  
	 * @param date1  
	 * @param date2  
	 * @return  
	 */
   public static int compare_date(Date date1, Date date2) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String strDate1 = DateUtils.getStrDate(date1);
        String strDate2 = DateUtils.getStrDate(date2);
        try {
            Date dt1 = df.parse(strDate1);
            Date dt2 = df.parse(strDate2);
            if (dt1.getTime() > dt2.getTime()) {
                return 1;
            } else if (dt1.getTime() < dt2.getTime()) {
                return -1;
            } else {
                return 0;
            }
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return 0;
    }
   
   /**
	 * 传入日期按天加减
	 * @param date
	 *        传入时间
	 *        
	 * @param day
	 *        加减的天数
	 * @return 天数       
	 * */
	public static Date addDay(Date date, int day) {
		  Calendar c1 = Calendar.getInstance();
		  c1.setTime(date);
		  c1.add(Calendar.DATE,day); 
		  return c1.getTime();
  }
   
	 /**
		 * 获取今天开始时间
		 * @return 今天开始时间
		 * */
	public static Date getStartTime(){  
        Calendar calendar = Calendar.getInstance();  
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));  
        calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));  
        calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));  
        calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));  
        return calendar.getTime();  
    }  
      
	/**
	 * 获取今天结束时间
	 * @return 今天结束时间
	 * */
	public static Date getEndTime(){  
        Calendar calendar = Calendar.getInstance();  
        calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));  
        calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));  
        calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));  
        calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));  
        return calendar.getTime();  
    }  
	
	 /**
	 * 获取本月开始天
	 * @return 本月开始天
	 * */
	public static Date getStartDay(){  
	    Calendar calendar = Calendar.getInstance();  
	    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));  
	    calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMinimum(Calendar.HOUR_OF_DAY));  
	    calendar.set(Calendar.MINUTE, calendar.getActualMinimum(Calendar.MINUTE));  
	    calendar.set(Calendar.SECOND, calendar.getActualMinimum(Calendar.SECOND));  
	    calendar.set(Calendar.MILLISECOND, calendar.getActualMinimum(Calendar.MILLISECOND));  
	    return calendar.getTime();  
	}  
  
	/**
	 * 获取本月结束天
	 * @return 本月结束天
	 * */
	public static Date getEndDay(){  
	    Calendar calendar = Calendar.getInstance();  
	    calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH)); 
	    calendar.set(Calendar.HOUR_OF_DAY, calendar.getActualMaximum(Calendar.HOUR_OF_DAY));  
	    calendar.set(Calendar.MINUTE, calendar.getActualMaximum(Calendar.MINUTE));  
	    calendar.set(Calendar.SECOND, calendar.getActualMaximum(Calendar.SECOND));  
	    calendar.set(Calendar.MILLISECOND, calendar.getActualMaximum(Calendar.MILLISECOND));  
	    return calendar.getTime();  
	}  
	
	public static Date getLastMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, -1);
        return cal.getTime();
    }
	
	
   public static void main(String[] args) {
	  System.out.println(getStringAlldate(getLastMonth(new Date())));
	  System.out.println(getStringAlldate(getEndDay()));
	  System.out.println(getStringAlldate(getEndTime()));
	  System.out.println(getStringAlldate(getStartTime()));
   }
}
